import { flysel } from '../plugin/flysel.js'
import { replaceUrl } from './tool.js'

let deviation = 0

export function setDeviation(v) {
    deviation = v
}

const anchors = flysel('.anchor')

const { documentElement: html } = document

flysel.document.on('scroll', () => {
    const anchor = location.hash
    for (let i = 0; i < anchors.length; i++) {
        const e = anchors.get(i);
        const top = e.offset().top
        if (top - html.scrollTop < deviation) {
            const id = '#' + e.e.id
            if (id === anchor) return id
            replaceUrl('./' + id)
            return id
        }
    }
    if (!anchor) return
    replaceUrl('./')
})
